import moment from 'moment';

/**
 * 用于匹配后台的查询
 * @param sort
 * @returns
 */
export function sortTran(sort: any) {
  const sorters = [];
  for (const property in sort) {
    if (sort.hasOwnProperty(property)) {
      if (sort[property] == 'ascend') {
        sorters.push({
          field: property,
          desc: false,
        });
      } else {
        sorters.push({
          field: property,
          desc: true,
        });
      }
    }
    // 可以扩展
  }
  return sorters;
}

export function filterTran(filter: any) {
  const sorters = [];
  for (const property in filter) {
    if (filter.hasOwnProperty(property)) {
      console.log('property,judge', property, filter.hasOwnProperty(property), filter[property]);
      if (
        property !== 'current' &&
        property !== 'pageSize' &&
        filter[property] !== '' &&
        filter[property] !== null &&
        filter[property] !== undefined
      ) {
        sorters.push({
          field: property,
          operator: 'like',
          value: filter[property],
          andOr: 'and',
        });
      }
    }
    console.log(sorters);
    // 可以扩展
  }
  return sorters;
}

export function filterTranNew(filter: any) {
  const sorters = [];
  // const IsDateValid = (val: any) => !Number.isNaN(new Date(val).valueOf());
  let v_proterty;
  const v_proterty_arr: boolean[] = [];
  for (const property in filter) {
    if (filter.hasOwnProperty(property)) {
      v_proterty = filter[property];
      if (
        property !== 'current' &&
        property !== 'pageSize' &&
        v_proterty !== null &&
        v_proterty !== undefined &&
        v_proterty !== ''
      ) {
        if (Array.isArray(v_proterty)) {
          if (property.startsWith('b_')) {
            v_proterty_arr.length = 0;
            v_proterty.forEach((element) => {
              v_proterty_arr.push(Boolean(element));
            });
            v_proterty = v_proterty_arr;
          }
          if (v_proterty.length > 0) {
            if (
              v_proterty.length == 2 &&
              (isNaN(v_proterty[0]) || isNaN(parseFloat(v_proterty[0]))) &&
              !isNaN(Date.parse(v_proterty[0]))
            ) {
              // console.log(typeof v_proterty[0]);
              if (typeof v_proterty[0] === 'object') {
                v_proterty_arr.length = 0;
                v_proterty.forEach((element) => {
                  v_proterty_arr.push(element.format('YYYY-MM-DD'));
                });
                v_proterty = v_proterty_arr;
              }

              let dateFormat = new Date(v_proterty[1]);
              dateFormat = new Date(dateFormat.setDate(dateFormat.getDate() + 1));
              sorters.push({
                field: property,
                operator: 'gt',
                value: v_proterty[0],
                andOr: 'and',
              });
              sorters.push({
                field: property,
                operator: 'lt',
                value: moment(dateFormat).format('YYYY-MM-DD'),
                andOr: 'and',
              });

              // sorters.push({
              //   field: property,
              //   operator: 'gte',
              //   value: v_proterty[0],
              //   andOr: 'and',
              // });
              // sorters.push({
              //   field: property,
              //   operator: 'lte',
              //   value: v_proterty[1],
              //   andOr: 'and',
              // });
            }
            // sorters.push({
            //   field: property,
            //   operator: 'gte',
            //   // value: new Date(Date.parse(v_proterty[0].replace(/-/g, '/'))),
            //   value: v_proterty[0],
            //   andOr: 'and',
            // });
            // sorters.push({
            //   field: property,
            //   operator: 'lt',
            //   // value: new Date(Date.parse(v_proterty[1].replace(/-/g, '/'))),
            //   value: v_proterty[1],
            //   andOr: 'and',
            // });
            else {
              sorters.push({
                field: property,
                operator: 'in',
                value: v_proterty,
                andOr: 'and',
              });
            }
          }
          //  } else if (!isNaN(parseFloat(v_proterty)) && isFinite(v_proterty)) {
          // } else if (typeof v_proterty == 'boolean') {
        } else if (property.startsWith('b_')) {
          sorters.push({
            field: property,
            operator: 'eq',
            value: Boolean(v_proterty),
            // value: v_proterty == '0' ? false : true,
            andOr: 'and',
          });
        } else if (property.startsWith('f_')) {
          sorters.push({
            field: property,
            operator: 'eq',
            value: v_proterty,
            // value: v_proterty == '0' ? false : true,
            andOr: 'and',
          });
        } else if (typeof v_proterty == 'string') {
          if (v_proterty.indexOf(' ') != -1) {
            sorters.push({
              field: property,
              operator: 'in',
              value: v_proterty.trim().split(' '),
              andOr: 'and',
            });
          } else {
            sorters.push({
              field: property,
              operator: 'like',
              value: v_proterty.trim(),
              andOr: 'and',
            });
          }
        }
        // else if (isNaN(v_proterty[0]) && !isNaN(Date.parse(v_proterty[0])))
        else {
          sorters.push({
            field: property,
            operator: 'eq',
            value: v_proterty,
            andOr: 'and',
          });
        }
      }
    }
    // 可以扩展
  }
  return sorters;
}
